package io.afu.baseframework.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class MicorsoftSpeaker {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36";


    private String msTryUrl = "https://azure.microsoft.com/en-gb/services/cognitive-services/text-to-speech/";


    private RestTemplate restTemplate = new RestTemplate();

    private HttpHeaders httpHeaders;

    private String authorization;


    private MicorsoftSpeaker() {
        httpHeaders = new HttpHeaders();
        httpHeaders.add("UserAgent",USER_AGENT);
    }

    public String getAuthorization() {
        if (authorization != null){
            return authorization;
        }
        String result = restTemplate.getForObject(msTryUrl,String.class);
//        logger.info("获取到的结果为："+result);
        String regexStr = "token: \"(.*?)\"";
        Pattern pattern = Pattern.compile(regexStr);
        Matcher matcher = pattern.matcher(result);
        while (matcher.find()){
            String group = matcher.group(0);
            if (group.contains("token")){
                authorization = group.replace("token:","").replace("\"","").trim();
            }
            logger.info(authorization);
        }
        return authorization;
    }

    public String makeWssUrl() {
        return "wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1?Authorization="+getAuthorization()+"&X-ConnectionId="+CommonUtils.uuid();
    }


    public static void main(String[] args) throws URISyntaxException {
        MicorsoftSpeaker micorsoftSpeaker = new MicorsoftSpeaker();
        String wssUrl = micorsoftSpeaker.makeWssUrl();
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        Map<String,String> params = new HashMap<>();
        params.put("User-Agent",USER_AGENT);
        WebsocketClient websocketClient = new WebsocketClient(
                new URI(wssUrl),params,"webclient"
        );
        websocketClient.connect();
        websocketClient.send("Path: speech.config\n" +
                "X-RequestId: B433C4522819416C980D2BCC7DB9AD07\n" +
                "X-Timestamp: 2022-08-17T01:57:30.625Z\n" +
                "Content-Type: application/json\n" +
                "\n" +
                "{\"context\":{\"system\":{\"name\":\"SpeechSDK\",\"version\":\"1.19.0\",\"build\":\"JavaScript\",\"lang\":\"JavaScript\"},\"os\":{\"platform\":\"Browser/MacIntel\",\"name\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54\",\"version\":\"5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54\"}}}");
    }























}
